/**
 * Created by hcadmin on 16/3/1.
 */
define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '用户名',
                dataIndex: 'name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var userName;
                    if(data[datarow].user==null)
                        userName='';
                    else
                        userName=data[datarow].user.name;
                    console.log('name',userName);
                    return "<span>" + userName + "</span>";
                }
            },
            {
                title: '问题',
                dataIndex: 'content',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '提问时间',
                dataIndex: 'create_date',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].create_date + "</span>";
                }
            },
            {
                title: '回答数',
                dataIndex: 'answer_num',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].answer_num + "</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var status = data[datarow].status;
                    var isAnswer = '专家回复';
                    var url = "'/pte/expert_answer?trouble_id="+data[datarow].trouble_id
                        +"'style='color: deepskyblue;'";
                    if (status){
                        isAnswer = '专家已回复';
                        var url = "";
                    }
                    //console.log('专家',isAnswer);
                    return "<span><a href='javascript:void(0)' style='color: red;' class='delete' act_id='"+data[datarow].trouble_id
                        +"'>删除</a><br><a href="+url
                        +">"+isAnswer+"</a>&nbsp&nbsp&nbsp<a href ='/pte/answer?trouble_id="
                        + data[datarow].trouble_id+"'style='color: orange;'>查看回答</a></span>";
                    //return "<span><a href='javascript:void(0)' style='color: red;' class='delete' act_id='"+data[datarow].trouble_id
                    //    +"'>删除</a><br><a href='/pte/expert_answer?trouble_id="+data[datarow].trouble_id
                    //    +"'style='color: deepskyblue;'>专家回复</a>&nbsp&nbsp&nbsp<a href ='/pte/answer?trouble_id="
                    //    + data[datarow].trouble_id+"' style='color: orange;'>查看回答</a></span>";
                }
            },

        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/pte/api/answer_lb", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var content=$("#wenda_content").val();
            var param="content=" +content;
            return param;
        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });
    });
    //删除问题
    $(document).on("click",".delete",function(){
        var act_id=$(this).attr("act_id");
        if(act_id){
            bootbox.confirm("确定要删除该问题？",function(result){
                if(result) {
                    IO.get("act/wenda_delete",{act_id:act_id},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/pte/answer_lb';
                            }
                        })
                    });
                }
            });
        }
    });

});